﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.SystemManagement
{
    public partial class UserPermission : System.Web.UI.Page
    {
        private IModulesBLL modulesBLL = IocContainer.Instance.Resolve<IModulesBLL>();
        private DataTable dtFunction = IocContainer.Instance.Resolve<IFunctionBLL>().GetAllFunction();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindModules();

                this.txtUserName.Attributes.Add("value", "admin");
                this.txtUserId.Value = "2";
                BindPages(2,Convert.ToInt32(this.ddlModules.SelectedValue));
            }
        }

        protected void BindModules()
        {
            DataTable dataTable = modulesBLL.GetModules();
            this.ddlModules.DataSource = dataTable;
            this.ddlModules.DataBind();
        }
        //查询
        protected void btnOk_Click(object sender, EventArgs e)
        {
            int userId=0;
            int.TryParse(this.txtUserId.Value.Trim(),out userId);
            int moduleId = 0;
            int.TryParse(this.ddlModules.SelectedValue,out moduleId);

            BindPages(userId, moduleId);
        }

        protected void BindPages(int userId, int moduleId)
        {
            DataTable dtModules = modulesBLL.GetPagesWithFinalFunctionAndAuthorityByUser(moduleId, userId);
            this.gvModuleView.DataSource = dtModules;
            this.gvModuleView.DataBind();
        }

        protected void ModuleView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBoxList AuthorityList = (CheckBoxList)e.Row.FindControl("AuthorityList");
                Label menu_ID = (Label)e.Row.FindControl("lab_ID");
                Label lab_Verify = (Label)e.Row.FindControl("lab_Verify");

                Boolean isEnabledLink = false;
                int n = dtFunction.Rows.Count;//系统功能个数
                //获取系统配置的权限列表，如果模块没有该权限，则禁用该权限
                for (int i = 0; i < n; i++)
                {
                    string functionID = dtFunction.Rows[i]["FunctionID"].ToString();
                    string functionName = dtFunction.Rows[i]["FunctionName"].ToString();
                    AuthorityList.Items.Add(new ListItem(functionName, functionID));
                    AuthorityList.Items[i].Enabled = false;

                    string pageFunction = this.gvModuleView.DataKeys[e.Row.RowIndex]["pageFunction"].ToString();
                    if (pageFunction.Contains("," + functionID + "-1"))
                    {
                        AuthorityList.Items[i].Enabled = true;
                        isEnabledLink = true;// 只要有一个功能可用，更新连接就可用
                    }

                    string pageAuthority = this.gvModuleView.DataKeys[e.Row.RowIndex]["pageAuthority"].ToString();
                    AuthorityList.Items[i].Selected = pageAuthority.Contains("," + functionID + "-1");
                }
            }
        }
    }
}
